package com.vastcom.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.vastcom.dao.ResourceDao;
import com.vastcom.dao.RoleDao;
import com.vastcom.dao.RoleresourceDao;
import com.vastcom.dao.UsersDao;
import com.vastcom.model.Resource;
import com.vastcom.model.Role;
import com.vastcom.model.Roleresource;
import com.vastcom.model.Users;
import com.vastcom.service.RcService;
@Service
public class RcServiceImpl implements RcService {
	
	@Autowired
	private UsersDao usersdao;
	@Autowired
	private RoleDao roledao;
	@Autowired
	private ResourceDao resourcedao;
	@Autowired
	private RoleresourceDao roleresourcedao;
	
	
	
	
	

	@Override
	public boolean addUser(Users u) {
		try {
			usersdao.save(u);
			return true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		
		
	}

	@Override
	public boolean deleteUser(Users u) {
		try {
			usersdao.delete(u);
			return true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		
		
	}

	@Override
	public Users getUser(Users u) {
		
		return usersdao.get(u.getId());
	}

	@Override
	public Users getUserById(String id) {
		
		return usersdao.get(id);
	}

	@Override
	public Users getUserByUsernameAndPassword(String username, String pass) {
		List<Users> ul=usersdao.getHQLZdyAnyP("from Users u where u.username=? and u.pass=?", username,pass);
		if(ul.size()>0){
			return ul.get(0);
		}
		else{
			return null;
		}
	}
	
	
	

	@Override
	public Users getUserByUsername(String username) {
		List<Users> ul=usersdao.getHQLZdyAnyP("from Users u where u.username=?", username);
		if(ul.size()>0){
			return ul.get(0);
		}
		else{
			return null;
		}
		
	}

	@Override
	public List<Users> getUsersByDept(String deptid) {
		
		return usersdao.getHQLZdyAnyP("from Users u where u.dpid=?", deptid);
	}

	@Override
	public List<Users> getUsersByRole(String roleid) {
		
		return usersdao.getHQLZdyAnyP("from Users u where u.roleid=?", roleid);
	}

	@Override
	public boolean updateUser(Users user) {
		try {
			usersdao.update(user);
			return true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		
		
	}

	@Override
	public boolean addRole(Role role) {
		try {
			roledao.save(role);
			return true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		
		
	}

	@Override
	public boolean deleteRole(Role role) {
		try {
			roledao.delete(role);
			return true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		
		
	}

	@Override
	public boolean updateRole(Role role) {
		try {
			roledao.update(role);
			return true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		
		
	}

	@Override
	public Role getRole(Role role) {
		
		return roledao.get(role.getId());
	}

	@Override
	public List<Role> getRoleList(int start, int num) {
		
		return roledao.getHQLZdy("from Role ", start, num);
	}

	@Override
	public List<Role> getRoleList() {
		
		return roledao.getHQLZdy("from Role");
	}
	
	
	

	@Override
	public String getRoleNameById(String roleid) {
		Role r=roledao.get(roleid);
		if(r!=null){
			return r.getRolename();
		}
		else{
			return "";
		}
		
	}

	@Override
	public boolean addResource(Resource resource) {
		try {
			resourcedao.save(resource);
			return true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		
		
	}

	@Override
	public boolean deleteResource(Resource resource) {
		try {
			resourcedao.delete(resource);
			return true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		
		
	}

	@Override
	public boolean updateResource(Resource resource) {
		try {
			resourcedao.update(resource);
			return true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		
		
	}

	@Override
	public Resource getResource(Resource resource) {
		
		return resourcedao.get(resource.getId());
	}

	@Override
	public List<Resource> getResourceListByType(String type) {
	
		return resourcedao.getHQLZdyAnyP("from Resource r where r.type=?", type);
	}
	
	
	
	

	@Override
	public String getResourceIdByPath(String path) {
		List<Resource> rl= resourcedao.getHQLZdyAnyP("from Resource r where r.path=?", path);
		if(rl.size()>0){
			return rl.get(0).getId();
		}
		else{
			return null;
		}
		
	}

	@Override
	public boolean addRoleresource(Roleresource roleresource) {
		try {
			roleresourcedao.save(roleresource);
			return true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		
	
	}

	@Override
	public boolean deleteRoleresource(Roleresource roleresource) {
		List<Roleresource> rl=roleresourcedao.getHQLZdyAnyP("from Roleresource"
				+ " r where r.roleid=? "
				+ "and r.resourceid=?", roleresource.getRoleid(),roleresource.getResourceid());
		if(rl.size()>0){
			roleresourcedao.delete(rl.get(0));
			return true;
		}
		else{
			return false;
		}
		
	}

	@Override
	public boolean hasResource(String roleid, String resourceid) {
		List<Roleresource> rl=roleresourcedao.getHQLZdyAnyP("from Roleresource"
				+ " r where r.roleid=? "
				+ "and r.resourceid=?", roleid,resourceid);
		if(rl.size()>0){
			
			return true;
		}
		else{
			return false;
		}
	}
	
	
	

}
